package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("user")
public class SmsController {

    @Autowired
    private SmsService smsService;

    @PostMapping("login")
    public ResponseEntity<ErrorResult> sendCheckCode(@RequestBody Map<String, String> pram) {
        String phone = pram.get("phone");
        System.out.println("test");
        /**
         * 返回的是null,说明原先没有验证码,但现在生成了验证码
         * 返回的 不是null,说明验证码的效果还在
         */
        ErrorResult result = null;
        try {
            result = smsService.sendCheckCode(phone);
            if (result == null) {
                return ResponseEntity.ok(null);
            } /*else if (result != null) {
                return ResponseEntity.status(100).body(result);
            }*/
        } catch (Exception e) {
            result.setErrCode("100");
            result.setErrMessage("验证码已生成");
        }
        return ResponseEntity.status(500).body(result);
    }
}
